package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.BusinessConfigBo;
import org.dromara.business.domain.vo.BusinessConfigVo;
import org.dromara.business.service.BusinessConfigService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务参数配置
 *
 * @author zlj
 * @date 2024-05-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/business")
public class BusinessConfigController extends BaseController {

    private final BusinessConfigService businessConfigService;

    /**
     * 查询业务参数配置列表
     */
    @SaCheckPermission("business:config:list")
    @GetMapping("/list")
    public TableDataInfo<BusinessConfigVo> list(BusinessConfigBo bo, PageQuery pageQuery) {
        return businessConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出业务参数配置列表
     */
    @SaCheckPermission("business:config:export")
    @Log(title = "业务参数配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusinessConfigBo bo, HttpServletResponse response) {
        List<BusinessConfigVo> list = businessConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "业务参数配置", BusinessConfigVo.class, response);
    }

    /**
     * 获取业务参数配置详细信息
     *
     * @param businessConfigId 主键
     */
    @SaCheckPermission("business:config:query")
    @GetMapping("/{businessConfigId}")
    public R<BusinessConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long businessConfigId) {
        return R.ok(businessConfigService.queryById(businessConfigId));
    }

    /**
     * 根据key获取业务参数配置详细信息
     *
     * @param configKey key
     */
    @SaIgnore
    @GetMapping("/getByKey")
    public R<List<BusinessConfigVo>> queryByKey(@NotNull(message = "key不能为空") String configKey) {
        return R.ok(businessConfigService.queryByKey(configKey));
    }


    /**
     * 新增业务参数配置
     */
    @SaCheckPermission("business:config:add")
    @Log(title = "业务参数配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusinessConfigBo bo) {
        return toAjax(businessConfigService.insertByBo(bo));
    }

    /**
     * 修改业务参数配置
     */
    @SaCheckPermission("business:config:edit")
    @Log(title = "业务参数配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusinessConfigBo bo) {
        return toAjax(businessConfigService.updateByBo(bo));
    }

    /**
     * 删除业务参数配置
     *
     * @param businessConfigIds 主键串
     */
    @SaCheckPermission("business:config:remove")
    @Log(title = "业务参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{businessConfigIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] businessConfigIds) {
        return toAjax(businessConfigService.deleteWithValidByIds(List.of(businessConfigIds), true));
    }

    /**
     * 修改状态
     */
    @SaCheckPermission("business:config:edit")
    @PutMapping("/updateStatus")
    public R<Void> updateStatus(Long businessConfigId) {
        return toAjax(businessConfigService.updateStatus(businessConfigId));
    }

    /**
     * 将经纬度解析为地址
     *
     * @param location gcj02坐标
     * @return 地址
     */
    @RepeatSubmit(interval = 1000, message = "正在获取定位,请稍后")
    @GetMapping("/getAddress")
    public R<String> getAddress(@RequestParam String location) {
        return R.data(businessConfigService.getAddress(location));
    }
}
